<template>
	<view class="content">
		<view class="form">
			<template v-for="v in formList">
				<rowInputVue v-if="v.type == 'input'" :item="v" v-model="form[v.field]" />
				<rowUploadVue v-if="v.type == 'upload'" :item="v" v-model="form[v.field]" />
			</template>
		</view>
		<view class="btn" @click="save">提交</view>
	</view>
</template>

<script>
	import rowInputVue from '@/components/form/rowInput.vue'
	import rowUploadVue from '@/components/form/rowUpload.vue'
	import {
		addShopCategory,
		editShopCategory
	} from '@/API/business.js'
	export default {
		components: {
			rowInputVue,
			rowUploadVue
		},
		data() {
			return {
				pid: 0,
				formList: [{
					type: 'input',
					title: '分类名称',
					field: 'name',
					place: '请输入分类名称',
					require: true,
					maxlength: 100,

				}, {
					type: 'upload',
					title: '分类图片',
					field: 'image',
					place: '请上传分类图片',
					require: true,
				}],
				form: {
					ids: 0,
					pid: 0,
					name: '',
					image: '',
				}
			}
		},
		onLoad(options) {
			if (options.data) {
				this.form = JSON.parse(options.data)
				this.form.ids = this.form.id
			}
			if (options.pid) {
				this.form.pid = options.pid
			}
		},
		methods: {
			save() {
				for (let i of this.formList) {
					if (i.require && !this.form[i.field]) {
						return this.$st.show(i.place)
					}
				}
				if (this.form.ids) { // 修改
					editShopCategory(this.form).then(res => {
						uni.navigateBack()
					})
				} else { // 添加
					addShopCategory(this.form).then(res => {
						uni.navigateBack()
					})
				}
			}
		}
	}
</script>

<style lang="less" scoped>
	.content {
		padding: 20rpx;

		.form {
			padding: 0 20rpx;
			background-color: #fff;
			border-radius: 20rpx;
		}

		.btn {
			position: fixed;
			bottom: 30rpx;
			left: 0;
			right: 0;
			margin: auto;
			width: 610rpx;
			height: 92rpx;
			line-height: 92rpx;
			text-align: center;
			background: linear-gradient(90deg, #DD551B, #F49355);
			border-radius: 46rpx;
			font-family: Source Han Sans CN;
			font-weight: 500;
			font-size: 34rpx;
			color: #FFFFFF;
		}
	}
</style>